package com.dslcode.cloud.common.enums.order;

import com.dslcode.cloud.common.enums.EnumJsonDeserializer;
import com.dslcode.cloud.common.enums.EnumJsonSerializer;
import com.dslcode.cloud.common.enums.IntEnum;
import com.dslcode.cloud.common.utils.JsonUtil;
import com.dslcode.cloud.common.utils.StringUtil;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;

import java.util.Map;

/**
 * Created by dongsilin on 2017/9/23.
 * 订单状态：0(已取消)10(默认):未付款;20:已付款;30:已发货;40:已收货;
 */
@JsonSerialize(using = EnumJsonSerializer.class)
@JsonDeserialize(using = EnumJsonDeserializer.class)
public enum OrderState implements IntEnum {
    canceled(0),
    not_pay(10),
    payed(20),
    deliveryed(30),
    received(40);

    public int value;

    OrderState(int value) {
        this.value = value;
    }

    @Override
//    @JsonValue
    public int getIntValue() {
        return this.value;
    }
    @Override
    public String getName() {
        return this.name();
    }
    @Override
    public OrderState fromJSON(String json) {
        if (StringUtil.isEmpty(json)) return null;
        Map<String, String> map = JsonUtil.readJson(json, Map.class);
        if (null == map || null == map.get("id")) return null;
        int id = Integer.valueOf(String.valueOf(map.get("id")));
        for (OrderState orderState : OrderState.values()){
            if (id == orderState.getIntValue()) return orderState;
        }
        return null;
    }

}